package com.kfm.base.jdbc;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

public class Demo {

    public static void main(String[] args) {
        try {
            // 加载驱动类
            DriverManager.registerDriver(new Driver());

            // 获取连接
            String url = "jdbc:mysql://localhost:3306/company_info";
            String user = "root";
            String pass = "root";
            try (Connection connection = DriverManager.getConnection(url, user, pass);
                 // 执行操作
                 Statement statement = connection.createStatement();) {

                String sql = "select * from dept";
                boolean execute = statement.execute(sql);
                if (execute) {
                    System.out.println("执行成功");
                    // sql 执行的结果
                    ResultSet resultSet = statement.getResultSet();

                    while (resultSet.next()) {
                        Object object = resultSet.getObject(1);
                        Object deptname = resultSet.getObject("deptname");

                        System.out.println(object + " - " + deptname);
                    }

                    resultSet.close();
                }


            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
